package carassius.DAL;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 *
 * @author siebz0r
 */
public class PictureRow
		extends Row<Integer>
{
	//<editor-fold defaultstate="collapsed" desc="Fields">
	private final int ID;
	private final String _path;
	private String _name, _comment;
	private final Object NAME_LOCK, COMMENT_LOCK;
	private final String SQL_UPDATE_NAME = "UPDATE picture SET name = ? where id = ?",
			SQL_UPDATE_COMMENT = "UPDATE picture SET comment = ? where id = ?";
	//</editor-fold>

	//<editor-fold defaultstate="collapsed" desc="Constructors">
	public PictureRow(ResultSet resultSet) throws SQLException
	{
		super(resultSet.getInt("id"));
		ID = resultSet.getInt("id");
		_path = resultSet.getString("path");
		_name = resultSet.getString("name");
		_comment = resultSet.getString("comment");
		NAME_LOCK = new Object();
		COMMENT_LOCK = new Object();
	}
	//</editor-fold>

	//<editor-fold defaultstate="collapsed" desc="Public Properties">
	public int getId()
	{
		return ID;
	}

	public String getPath()
	{
		return _path;
	}

	public String getComment()
	{
		synchronized (COMMENT_LOCK)
		{
			return _comment;
		}
	}

	public boolean setComment(String comment)
	{
		boolean result = false;
		synchronized (COMMENT_LOCK)
		{
			if (comment == null ? _comment != null : !comment.equals(_comment))
			{
				try
				{
					PreparedStatement st = Database.prepareStatement(SQL_UPDATE_COMMENT);
					st.setObject(1, (comment != null) ? comment : null, java.sql.Types.VARCHAR);
					st.setInt(2, ID);

					if (st.executeUpdate() == 1)
					{
						_comment = comment;
						result = true;
					}
					st.close();
				}
				catch (SQLException ex)
				{
					System.out.println(ex.toString());
				}
			}
		}
		return result;
	}

	public String getName()
	{
		synchronized (NAME_LOCK)
		{
			return _name;
		}
	}

	public boolean setName(String name)
	{
		boolean result = false;
		synchronized (NAME_LOCK)
		{
			if (name == null ? _name != null : !name.equals(_name))
			{
				try
				{
					PreparedStatement st = Database.prepareStatement(SQL_UPDATE_NAME);
					st.setObject(1, (name != null) ? name : null, java.sql.Types.VARCHAR);
					st.setInt(2, ID);

					if (st.executeUpdate() == 1)
					{
						_name = name;
						result = true;
					}
					st.close();
				}
				catch (SQLException ex)
				{
					System.out.println(ex.toString());
				}
			}
		}
		return result;
	}
	//</editor-fold>

	//<editor-fold defaultstate="collapsed" desc="Public Methods">
	@Override
	public boolean equals(Object obj)
	{
		if (obj == null)
		{
			return false;
		}
		if (getClass() != obj.getClass())
		{
			return false;
		}
		final PictureRow other = (PictureRow) obj;
		if (this.ID != other.ID)
		{
			return false;
		}
		if ((this._path == null) ? (other._path != null) : !this._path.equals(other._path))
		{
			return false;
		}
		if ((this._name == null) ? (other._name != null) : !this._name.equals(other._name))
		{
			return false;
		}
		if ((this._comment == null) ? (other._comment != null) : !this._comment.equals(other._comment))
		{
			return false;
		}
		return true;
	}

	@Override
	public int hashCode()
	{
		int hash = 7;
		hash = 31 * hash + this.ID;
		hash = 31 * hash + (this._path != null ? this._path.hashCode() : 0);
		hash = 31 * hash + (this._name != null ? this._name.hashCode() : 0);
		hash = 31 * hash + (this._comment != null ? this._comment.hashCode() : 0);
		return hash;
	}
	//</editor-fold>
}
